<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight 1.6.3
by Lorenzo Bettini, bettini@gnu.org
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>code/Ch2/mail/Telephone.java</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<pre>
<tt>
01: <b><font color=#000080>import</font></b> java<font color=#990000>.</font>io<font color=#990000>.</font>BufferedReader<font color=#990000>;</font>
02: <b><font color=#000080>import</font></b> java<font color=#990000>.</font>io<font color=#990000>.</font>IOException<font color=#990000>;</font>
03: 
04: <i><font color=#9A1900>/**</font></i>
05: <i><font color=#9A1900>   A telephone that takes simulated keystrokes and voice input</font></i>
06: <i><font color=#9A1900>   from the user and simulates spoken text.</font></i>
07: <i><font color=#9A1900>*/</font></i>
08: <b><font color=#0000FF>public</font></b> <b><font color=#0000FF>class</font></b> Telephone
09: <font color=#FF0000>{</font>
10:    <i><font color=#9A1900>/**</font></i>
11: <i><font color=#9A1900>      Construct phone object.</font></i>
12: <i><font color=#9A1900>      @param aReader that reads text from a character-input stream</font></i>
13: <i><font color=#9A1900>   */</font></i>
14:    <b><font color=#0000FF>public</font></b> <b><font color=#000000>Telephone</font></b><font color=#990000>(</font>BufferedReader aReader<font color=#990000>)</font>
15:    <font color=#FF0000>{</font>
16:       reader <font color=#990000>=</font> aReader<font color=#990000>;</font>
17:    <font color=#FF0000>}</font>
18: 
19:    <i><font color=#9A1900>/**</font></i>
20: <i><font color=#9A1900>      Speak a message to System.out.</font></i>
21: <i><font color=#9A1900>      @param output the text that will be "spoken"</font></i>
22: <i><font color=#9A1900>   */</font></i>
23:    <b><font color=#0000FF>public</font></b> <font color=#009900>void</font> <b><font color=#000000>speak</font></b><font color=#990000>(</font>String output<font color=#990000>)</font>
24:    <font color=#FF0000>{</font>
25:       System<font color=#990000>.</font>out<font color=#990000>.</font><b><font color=#000000>println</font></b><font color=#990000>(</font>output<font color=#990000>)</font><font color=#990000>;</font>
26:    <font color=#FF0000>}</font>
27: 
28:    <i><font color=#9A1900>/**</font></i>
29: <i><font color=#9A1900>      Loops reading user input and passes the input to the </font></i>
30: <i><font color=#9A1900>      Connection object's methods dial, record or hangup.</font></i>
31: <i><font color=#9A1900>      @param c the connection that connects this phone to the </font></i>
32: <i><font color=#9A1900>      voice mail system</font></i>
33: <i><font color=#9A1900>   */</font></i>
34:    <b><font color=#0000FF>public</font></b> <font color=#009900>void</font> <b><font color=#000000>run</font></b><font color=#990000>(</font>Connection c<font color=#990000>)</font> <b><font color=#0000FF>throws</font></b> IOException
35:    <font color=#FF0000>{</font>
36:       <font color=#009900>boolean</font> more <font color=#990000>=</font> true<font color=#990000>;</font>
37:       <b><font color=#0000FF>while</font></b> <font color=#990000>(</font>more<font color=#990000>)</font>
38:       <font color=#FF0000>{</font>
39:          String input <font color=#990000>=</font> reader<font color=#990000>.</font><b><font color=#000000>readLine</font></b><font color=#990000>(</font><font color=#990000>)</font><font color=#990000>;</font>
40:          <b><font color=#0000FF>if</font></b> <font color=#990000>(</font>input <font color=#990000>=</font><font color=#990000>=</font> <b><font color=#0000FF>null</font></b><font color=#990000>)</font> <b><font color=#0000FF>return</font></b><font color=#990000>;</font>
41:          <b><font color=#0000FF>if</font></b> <font color=#990000>(</font>input<font color=#990000>.</font><b><font color=#000000>equalsIgnoreCase</font></b><font color=#990000>(</font><font color=#FF0000>"H"</font><font color=#990000>)</font><font color=#990000>)</font>
42:             c<font color=#990000>.</font><b><font color=#000000>hangup</font></b><font color=#990000>(</font><font color=#990000>)</font><font color=#990000>;</font>
43:          <b><font color=#0000FF>else</font></b> <b><font color=#0000FF>if</font></b> <font color=#990000>(</font>input<font color=#990000>.</font><b><font color=#000000>equalsIgnoreCase</font></b><font color=#990000>(</font><font color=#FF0000>"Q"</font><font color=#990000>)</font><font color=#990000>)</font>
44:             more <font color=#990000>=</font> false<font color=#990000>;</font>
45:          <b><font color=#0000FF>else</font></b> <b><font color=#0000FF>if</font></b> <font color=#990000>(</font>input<font color=#990000>.</font><b><font color=#000000>length</font></b><font color=#990000>(</font><font color=#990000>)</font> <font color=#990000>=</font><font color=#990000>=</font> <font color=#993399>1</font> 
46:             <font color=#990000>&amp;</font><font color=#990000>&amp;</font> <font color=#FF0000>"1234567890#"</font><font color=#990000>.</font><b><font color=#000000>indexOf</font></b><font color=#990000>(</font>input<font color=#990000>)</font> <font color=#990000>&gt;</font><font color=#990000>=</font> <font color=#993399>0</font><font color=#990000>)</font>
47:             c<font color=#990000>.</font><b><font color=#000000>dial</font></b><font color=#990000>(</font>input<font color=#990000>)</font><font color=#990000>;</font>
48:          <b><font color=#0000FF>else</font></b>            
49:             c<font color=#990000>.</font><b><font color=#000000>record</font></b><font color=#990000>(</font>input<font color=#990000>)</font><font color=#990000>;</font>
50:       <font color=#FF0000>}</font>
51:    <font color=#FF0000>}</font>
52: 
53:    <b><font color=#0000FF>private</font></b> BufferedReader reader<font color=#990000>;</font>
54: <font color=#FF0000>}</font>
</tt>
</pre>
</body>
</html>
